<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Spatie\Menu\Laravel\Html;
use Spatie\Menu\Laravel\Menu;
use App\Models\Menu as MenuModel;

class DashboardController extends Controller
{
    public function index()
    {
        $items = [
            '/admin' => ['label' => '仪表盘', 'icon' => 'fa-link'],
            '/#' => ['label' => '测试', 'icon' => 'fa-link'],
//            '/#2' => [
//                '/#21' => '子菜单1',
//                '/#22' => '子菜单2'
//            ]
        ];
        $test = Menu::build($items, function ($menu, $label, $link) {
//            if (is_array($label)){
//                Menu::build($label, function ($subMenu, $subLabel, $subLink) {
//
//                }
//            }
            $menu->add(Html::raw(sprintf('<a href="%s"><i class="fa %s"></i> <span>%s</span></a>', $link, $label['icon'], $label['label'])));
        })
            ->setAttribute('data-widget', 'tree')
            ->addClass('sidebar-menu');
        $this->test();
        return view('admin.dashboard', compact('test'));
    }

    protected function test()
    {
//        $root = MenuModel::create(['name' => 'The Root of All Evil']);
//
//        $dragons = MenuModel::create(['name' => 'Here Be Dragons']);
//        $dragons->makeChildOf($root);

//        $monsters = MenuModel::find(1);
//        $nestedList = MenuModel::getNestedList('name','id','-');
//        $monsters = new MenuModel(['name' => 'root2']);
//        $monsters->save();
//        $test =  $monsters->getDescendantsAndSelf();
//        $monsters->makeChildOf(MenuModel::find(2));
//        $root = MenuModel::find(2);
//        $monsters = MenuModel::find(7);

//        $monsters->makeSiblingOf($root);
//        $monsters->moveLeft($root);

//        $monsters->makeSiblingOf($dragons);
//
//        $demons = MenuModel::where('name', '=', 'demons')->first();
//        $demons->moveToLeftOf($dragons);
//        dd($nestedList);
    }
}
